﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
//using SX.Utils.Dapper;
//using System.Data;
//using System.Data.SqlClient;
//using IBatisNet.DataMapper.MappedStatements;

//namespace SX.Utils.Dapper
//{
//    public static class DapperExtend
//    {
//        private static IBatisNet.DataMapper.ISqlMapper _sqlMapper = null;
//        private static IBatisNet.DataMapper.ISqlMapper Instance {
//            get {
//                if (_sqlMapper == null)
//                {
//                    _sqlMapper = IBatisNet.DataMapper.Mapper.Instance();
//                }
//                return _sqlMapper;
//            }
//        }
//        private static IBatisNet.DataMapper.ISqlMapSession Session
//        {
//            get {
//                IBatisNet.DataMapper.ISqlMapSession session = Instance.LocalSession;
//                //if (session == null)
//                //{
//                //    session = Instance.CreateSqlMapSession();
//                //}
//                return session;
//            }
//        }
//        public static IMappedStatement GetStateMent(string id) {
//            return Instance.GetMappedStatement(id);
//        }
//        public static string GetSql(IMappedStatement statement, object parameter)
//        {  
//            var request = statement.Statement.Sql.GetRequestScope(statement, parameter, Session);
//            return request.PreparedStatement.PreparedSql;
//        }

//        public static string GetSql(string statementName, object parameter) {
//            var stateMent = GetStateMent(statementName);
//            return GetSql(stateMent, parameter);
//        }
//        public static object Scalar4Sate(this SqlConnection conn, string statementName, object parameter)
//        {
//            return conn.ExecuteScalar(GetSql(statementName, parameter), parameter);
//        }

//        public static T Scalar4Sate<T>(this SqlConnection conn, string statementName, object parameter) {
//            return conn.ExecuteScalar<T>(GetSql(statementName, parameter), parameter);
//        }
       
//        public static T Insert4State<T>(this SqlConnection conn, string statementName, object parameter) {

//            T generatedKey = default(T);
//            IBatisNet.DataMapper.Configuration.Statements.SelectKey selectKeyStatement = null;
//            var _statement = GetStateMent(statementName);
//            var request = _statement.Statement.Sql.GetRequestScope(_statement, parameter, Session);

//            if (_statement.Statement is IBatisNet.DataMapper.Configuration.Statements.Insert)
//            {
//                selectKeyStatement = ((IBatisNet.DataMapper.Configuration.Statements.Insert)_statement.Statement).SelectKey;
//            }

//            if (selectKeyStatement != null && !selectKeyStatement.isAfter)
//            { 
//                generatedKey = Scalar4Sate<T>(conn, selectKeyStatement.Id, parameter); 
//            }

//            if (_statement.Statement is IBatisNet.DataMapper.Configuration.Statements.Insert)
//            {
//                conn.Execute(GetSql(_statement, parameter), parameter);
//            }
//            // Retrieve output parameter if the result class is specified
//            else if (_statement.Statement is IBatisNet.DataMapper.Configuration.Statements.Procedure)
//            { 
//                DynamicParameters p = new DynamicParameters(parameter);
//                p.Add("@returnKey", direction: ParameterDirection.ReturnValue);
//                conn.Execute(GetSql(_statement, parameter), p, commandType: CommandType.StoredProcedure);
//                generatedKey = p.Get<T>("@returnKey");
//            }
//            else
//            {
//                generatedKey = conn.ExecuteScalar<T>(GetSql(_statement, parameter), parameter);
//            }

//            if (selectKeyStatement != null && selectKeyStatement.isAfter)
//            {
//                generatedKey = Scalar4Sate<T>(conn, selectKeyStatement.Id, parameter); 
//            }
//            return generatedKey;
             
//        } 

//        public static int Execute4State(this SqlConnection conn, string statementName, object parameter)
//        {
//            return conn.Execute(GetSql(statementName, parameter), parameter);
//        }

//        public static IEnumerable<T> Query4State<T>(this SqlConnection conn, string statementName, object parameter) {
//            return conn.Query<T>(GetSql(statementName, parameter), parameter);
//        }

//        public static IEnumerable<dynamic> Query4State(this SqlConnection conn, string statementName, object parameter)
//        {
//            return conn.Query(GetSql(statementName, parameter), parameter);
//        }

//        public static T QuerySingle4State<T>(this SqlConnection conn, string statementName, object parameter)
//        {
//            return conn.QuerySingle<T>(GetSql(statementName, parameter), parameter);
//        }

//        public static dynamic QuerySingle4State(this SqlConnection conn, string statementName, object parameter)
//        {
//            return conn.QuerySingle(GetSql(statementName, parameter), parameter);
//        }

//        public static IDataReader ExecuteReader4State(this SqlConnection conn, string statementName, object parameter)
//        {
//            return conn.ExecuteReader(GetSql(statementName, parameter), parameter);
//        } 
//    }
//}
